/*
 * Copyright 2009 Johnson Consulting Services
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.johnsoncs.gwt.widgets.samples.client.mock;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.johnsoncs.gwt.widgets.client.common.DeferredImage;
import com.johnsoncs.gwt.widgets.client.dynamictable.ActionDefinition;
import com.johnsoncs.gwt.widgets.client.dynamictable.ColumnDefinition;
import com.johnsoncs.gwt.widgets.client.dynamictable.ColumnRenderer;
import com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable;
import com.johnsoncs.gwt.widgets.client.dynamictable.TableCell;
import com.johnsoncs.gwt.widgets.client.dynamictable.TableDefinition;
import com.johnsoncs.gwt.widgets.client.dynamictable.actions.ExportAction;
import com.johnsoncs.gwt.widgets.client.dynamictable.actions.ShowSettingsAction;
import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportFormatDefinition;
import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportFormats;
import com.johnsoncs.gwt.widgets.client.dynamictable.export.MappedExportRequestBuilder;

public class MyTableDefinition extends TableDefinition<MyRowObject> {

	public MyTableDefinition(final SelectionMode selectionMode) {
		//		super(SelectionMode.MULTI_ITEM, "myTable", DefaultTableImages.INSTANCE);
		super(selectionMode, "myTable");
		super.addColumn(new ColumnDefinition<MyRowObject>("MYROW_ID", true, true, new ColumnRenderer<MyRowObject>() {

			@Override
			public String getDisplayName() {
				return "ID";
			}

			@Override
			public void onRedraw(final DynamicTable<MyRowObject> table, final Widget headerFooterWidget) {
				// do nothing
			}

			@Override
			public void renderCell(final DynamicTable<MyRowObject> table, final TableCell cell, final MyRowObject value) {
				cell.setText(value.getId());
			}

			@Override
			public void renderHeader(final DynamicTable<MyRowObject> table, final TableCell cell) {
				cell.setText(getDisplayName());
			}

		}));
		super.addColumn(new ColumnDefinition<MyRowObject>("FIRST_NAME", true, true, new ColumnRenderer<MyRowObject>() {

			@Override
			public String getDisplayName() {
				return "First Name";
			}

			@Override
			public void onRedraw(final DynamicTable<MyRowObject> table, final Widget headerFooterWidget) {
				// do nothing
			}

			@Override
			public void renderCell(final DynamicTable<MyRowObject> table, final TableCell cell, final MyRowObject value) {
				cell.setText(value.getFirstName());
			}

			@Override
			public void renderHeader(final DynamicTable<MyRowObject> table, final TableCell cell) {
				cell.setText(getDisplayName());
			}
		}));

		super.addColumn(new ColumnDefinition<MyRowObject>("LAST_NAME", true, true, new ColumnRenderer<MyRowObject>() {

			@Override
			public String getDisplayName() {
				return "Last Name";
			}

			@Override
			public void onRedraw(final DynamicTable<MyRowObject> table, final Widget headerFooterWidget) {
				// do nothing
			}

			@Override
			public void renderCell(final DynamicTable<MyRowObject> table, final TableCell cell, final MyRowObject value) {
				cell.setText(value.getLastName());
			}

			@Override
			public void renderHeader(final DynamicTable<MyRowObject> table, final TableCell cell) {
				cell.setText(getDisplayName());
			}
		}));

		super.addColumn(new ColumnDefinition<MyRowObject>("STATUS", false, true, new ColumnRenderer<MyRowObject>() {

			@Override
			public String getDisplayName() {
				return "Status";
			}

			@Override
			public void onRedraw(final DynamicTable<MyRowObject> table, final Widget headerFooterWidget) {
				// do nothing
			}

			@Override
			public void renderCell(final DynamicTable<MyRowObject> table, final TableCell cell, final MyRowObject value) {
				final String title = (value.isStatus()) ? "status ok" : "status warning";
				final Image img = new DeferredImage("img" + (value.isStatus() ? "0" : "1") + ".png");
				img.setTitle(title);
				cell.setWidget(img);
			}

			@Override
			public void renderHeader(final DynamicTable<MyRowObject> table, final TableCell cell) {
				cell.setText(getDisplayName());
			}
		}, ExportAction.NON_EXPORTABLE));

		//		for (int i = 0; i < 10; i++) {
		//			super.addColumn(new MyLinkColumn("link" + (i + 1)));
		//		}

		super.addAction(new ActionDefinition<MyRowObject>("Settings", new ShowSettingsAction<MyRowObject>()));

		final String rowMapper = "com.johnsoncs.gwt.widgets.samples.server.mock.MyRowObjectMapper";
		final ExportAction<MyRowObject> export = new ExportAction<MyRowObject>(MockRemoteTableModel.Util
				.getExportService(), rowMapper);

		final MappedExportRequestBuilder<MyRowObject> builder = new MappedExportRequestBuilder<MyRowObject>();
		export.addExportFormat(new ExportFormatDefinition<MyRowObject>("Comma Separated", ExportFormats.INSTANCE
				.getCsvFormat(), builder));
		export.addExportFormat(new ExportFormatDefinition<MyRowObject>("Tab Separated", ExportFormats.INSTANCE
				.getTabDelimitedFormat(), builder));


		super.addAction(new ActionDefinition<MyRowObject>("Export", export));
	}

	// example code for creating a "static column"
	//	private static class MyLinkColumn extends StaticColumnDefinition<MyRowObject> {
	//	private static class MyLink extends Hyperlink {
	//		private MyRowObject value;
	//
	//		public MyLink(final String name) {
	//			super(name, "");
	//			super.addClickHandler(new ClickHandler() {
	//
	//				@Override
	//				public void onClick(final ClickEvent arg0) {
	//					Window.alert("You clicked: " + value.getId());
	//				}
	//			});
	//		}
	//
	//		public void setValue(final MyRowObject value) {
	//			this.value = value;
	//		}
	//	}
	//
	//	/**
	//	 * @param name
	//	 * @param attributes
	//	 */
	//	public MyLinkColumn(final String name, final ColumnAttribute... attributes) {
	//		super(name, attributes);
	//	}
	//
	//	/* (non-Javadoc)
	//	 * @see com.johnsoncs.gwt.widgets.client.dynamictable.StaticColumnDefinition#getCellWidget(com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable)
	//	 */
	//	@Override
	//	public Widget getCellWidget(final DynamicTable<MyRowObject> table) {
	//		return new MyLink(super.getName());
	//	}
	//
	//	/* (non-Javadoc)
	//	 * @see com.johnsoncs.gwt.widgets.client.dynamictable.StaticColumnDefinition#getHeaderFooterWidget(com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable)
	//	 */
	//	@Override
	//	public Widget getHeaderFooterWidget(final DynamicTable<MyRowObject> table) {
	//		return null;
	//	}
	//
	//	/* (non-Javadoc)
	//	 * @see com.johnsoncs.gwt.widgets.client.dynamictable.StaticColumnDefinition#updateCellWidget(com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable, com.google.gwt.user.client.ui.Widget, java.lang.Object)
	//	 */
	//	@Override
	//	public void updateCellWidget(final DynamicTable<MyRowObject> table, final Widget widget, final MyRowObject value) {
	//		((MyLink) widget).setValue(value);
	//	}
	//
	//	/* (non-Javadoc)
	//	 * @see com.johnsoncs.gwt.widgets.client.dynamictable.StaticColumnDefinition#updateHeaderFooterWidget(com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable, com.google.gwt.user.client.ui.Widget)
	//	 */
	//	@Override
	//	public void updateHeaderFooterWidget(final DynamicTable<MyRowObject> table, final Widget widget) {
	//		// do nothing
	//	}
	//
	//}

}